/*
LKSHELL.C - Just starts up the link
*/

#include <plib.h>
#include <wlib.h>

LOCAL_D WSERV_SPEC wSpec;
LOCAL_D UINT wMainGc;
LOCAL_D UINT wMainWid;
LOCAL_D INT FontHeight;
LOCAL_D INT FontAscent;

LOCAL_C VOID SetFontHeight(VOID)
    {
    G_FONT_INFO info;

    gFontInfo(WS_FONT_SYSTEM,0,&info);
    FontHeight=info.height;
    FontAscent=info.ascent;
    }

LOCAL_C VOID CDECL PrintLine(INT line,INT align,TEXT *fmt,...)
    {
    INT len;
    P_RECT box;
    TEXT b[80];

    box.tl.x=4;
    box.br.x=wSpec.conn.info.pixels.x-4;
    box.tl.y=FontHeight*line+4;
    box.br.y=box.tl.y+FontHeight;
    len=p_atob(&b[0],fmt,&fmt+1);
    gPrintBoxText(&box,FontAscent,align,0,&b[0],len);
    }

LOCAL_C VOID MainEventLoop(VOID)
    {
    WS_EV event;

    SetFontHeight();
    for (;;)
        {
        wGetEventWait(&event);
        if (event.type==WM_REDRAW)
            {
            wValidateWin(wMainWid);
            gBorder(W_BORD_SHADOW_D|W_BORD_SHADOW_ON);
            PrintLine(0,G_TEXT_ALIGN_LEFT,"Free memory: %dKbytes",p_sgfree()>>6);
            }
        else if (event.type==WM_KEY && event.p.key.keycode=='\r')
            wInvalidateWin(wMainWid);
        }
    }

GLDEF_C VOID main(VOID)
    {
    INT NotifierPid;
    WORD stat;

    p_setonevent(TRUE);
    wConnect(&wSpec,0,W_CONNECT_PRIORITY);
    wSystem(WSERV_FLAG_NO_NOTIFIER_REBOOT,WSERV_FLAG_NO_NOTIFIER_REBOOT);
    if ((NotifierPid=p_pidfind("SYS$NTFY.*"))>0)
        {
        p_logona(NotifierPid,&stat);
        p_pterminate(NotifierPid,0);
        p_waitstat(&stat);
        }
    wSystem(WSERV_FLAG_HOOK_NOTIFIER|WSERV_FLAG_LOW_BATTERY_WARNINGS|WSERV_FLAG_HUNG_UP_SW,
        WSERV_FLAG_HOOK_NOTIFIER|WSERV_FLAG_LOW_BATTERY_WARNINGS|WSERV_FLAG_HUNG_UP_SW);
    if (p_pidfind("SYS$NCP.*")<0)
        p_presume(p_execc("ROM::LINK",NULL,0));
    wMainWid=wCreateWindow(0,0,0,1);
    wsCreateClock(wMainWid,WS_CLOCK_WITH_DATE|WS_CLOCK_WITH_SECONDS,104,66,0);
    wInitialiseWindowTree(wMainWid);
    wMainGc=gCreateGC0(wMainWid);
    MainEventLoop();
    }
